
//  QuickLookCell.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "QuickLookCell.h"
#import "Header.h"
#import "Model.h"

#import "CommentController.h"

#import "QuickLookViewController.h"

#import "TableViewController.h"
#import "LandController.h"
@interface QuickLookCell()


{
    NSIndexPath* _indexPath;
    BOOL _isExisted;
    BOOL _isSelected;


}

@property (weak, nonatomic) IBOutlet UILabel *topicTitle;


//昵称

@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UIImageView *thumber;


//头像
@property (weak, nonatomic) IBOutlet UIImageView *userAvatar_url;


@property (weak, nonatomic) IBOutlet UIImageView *cover_image_url;



//第几话
@property (weak, nonatomic) IBOutlet UILabel *title;


//底部 右到左
@property (weak, nonatomic) IBOutlet UILabel * comments_count;


//分享个数
@property (weak, nonatomic) IBOutlet UILabel *shared_count;


//点赞个数
@property (weak, nonatomic) IBOutlet UILabel *likes_count;
@property (weak, nonatomic) IBOutlet UILabel *special;
@property (weak, nonatomic) IBOutlet UIView *commentView

;

@property(nonatomic,strong)Model* model;

@property (weak, nonatomic) IBOutlet UIView *goodTapView;

@property (weak, nonatomic) IBOutlet UIImageView *goodHand;


@property (weak, nonatomic) IBOutlet UIView *shareView;


@property(nonatomic,strong)NSIndexPath* indexPath;
@end
@implementation QuickLookCell

- (void)awakeFromNib {
    // Initialization code
    
    _userAvatar_url.layer.cornerRadius=15;
    
    _userAvatar_url.layer.masksToBounds = YES;
   _goodTapView.tag=0;
    
    
    UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
    
    [_commentView addGestureRecognizer:tap];
    
    //thumb点击放大手势
    UITapGestureRecognizer* goodTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goodTap:)];
    
    
    [_goodTapView addGestureRecognizer:goodTap];
    
    
//点击分享手势
    
    
    
    UITapGestureRecognizer* shareTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shareTap:)];
    
    
    [_shareView addGestureRecognizer:shareTap];
    
    
}

//左侧分享点击
- (void)shareTap:(UITapGestureRecognizer*)shareTap
{

    
    UIViewController* delegate;
    
    if (self.TableViewControllerDelegate) {
        delegate=self.TableViewControllerDelegate;
    }else
    {
        delegate=self.delegate;

    
    }
    
    Model* model=self.delegate.dataArray[_indexPath.section];
    
    NSString* shareStr=[NSString stringWithFormat:@"最近在极致漫画上看到一部超好看的漫画:%@ ,和我一起来看吧~ 完整内容戳: %@",model.topic[@"title"],model.url];
    
    UIImage*shareIamge= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.cover_image_url]]];

    
    [UMSocialSnsService presentSnsIconSheetView:delegate
                                         appKey:@"5606db0ce0f55a58d1003a21"
                                      shareText:shareStr
                                     shareImage:shareIamge
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToSms,UMShareToRenren,nil]
                                       delegate:nil];









}

- (void)goodTap:(UITapGestureRecognizer*)tap{

    //无网络连接 无法点击
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    BOOL isConnection = 0;
    
    isConnection=[userdefault boolForKey:@"isConnection"];
    
    if (!isConnection) {
        return;
    }

    
    BOOL isLand=[userdefault boolForKey:ISLAND];//BOOL
    
    if (!isLand) {
        LandController* vc=[[LandController alloc]init];
        
        [self.delegate presentViewController:vc animated:YES completion:nil];
        
        
//        [ProgressHUD showError:@"未登录" Interaction:YES];
    
        return;
    }
    
    
    static int i=1;
    if (self.TableViewControllerDelegate) {
        return;
    }
    
    Model* model= self.delegate.dataArray[_indexPath.section];

//    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];

    QuickLookCell* cell=[self.delegate.tableView cellForRowAtIndexPath:_indexPath];

    [UIView animateWithDuration:0.2 animations:^{
    
        _goodHand.transform= CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0);
        
        
        [mutableArray enumerateObjectsUsingBlock:^(NSNumber* obj, NSUInteger idx, BOOL *stop) {
            
            if ([model.topic[@"id"] isEqualToNumber:obj]) {
                
                _isSelected=YES;
                _isExisted=YES;

            }
            
                
        }];
        
        
        if (!_isSelected) {
            
          
            
            
            _goodHand.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];
            _likes_count.textColor=JZJYELLOW;
            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            [mutaArray addObjectsFromArray:mutableArray];
            
            
            
            if (_isExisted==NO) {
                [mutaArray addObject:_model.topic[@"id"]];
                [userdefault setObject:mutaArray forKey:IDArray];
                [userdefault synchronize];
                _isExisted=YES;
                
            }
            
            
            
            
            cell.likes_count.text=[NSString stringWithFormat:@"%zd",[cell.likes_count.text integerValue]+1];
            _isSelected=YES;
 
        }else{
            

            
            _likes_count.textColor=TEXTCOLOR;



            _goodHand.image=[UIImage imageNamed:@"quickLook2.png"];


            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            [mutaArray addObjectsFromArray:mutableArray];
            [mutaArray removeObject:model.topic[@"id"]];
            
            [userdefault setObject:mutaArray forKey:IDArray];
            
            
            [userdefault synchronize];

            cell.likes_count.text=[NSString stringWithFormat:@"%zd",[cell.likes_count.text integerValue]-1];

            _isSelected=NO;
            _isExisted=NO;

        }
        
        
        
       
        Model* model= self.delegate.dataArray[_indexPath.section];
       
        
        [DownLoad  getWetherDataWithID:model.topic[@"id"] andBlock:^(NSDictionary* obj, NSError *error) {
            
            {
                
                NSString* str=obj[@"message"];
                
                
                if ([str isEqual:@"OK"]) {
                    
                    
             
                }
        

            }
        }];
        
        
        


        
        
        
    } completion:^(BOOL finished) {
        
        
        [UIView animateWithDuration:0.2 animations:^{
            _goodHand.transform= CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);

      }];

//        [self.delegate.tableView reloadData];

    }];
    

    JZJLog(@"%@",[userdefault objectForKey:IDArray]);
    
    i++;
}

- (void)tap:(UITapGestureRecognizer*)tap
{



    CommentController* vc=[[CommentController alloc]init];
    
    vc.hidesBottomBarWhenPushed=YES;
    vc.model=_model;
    
    
    if (self.TableViewControllerDelegate) {
        [self.TableViewControllerDelegate presentViewController:vc animated:YES completion:nil];

    }else
    {
    
    [self.delegate presentViewController:vc animated:YES completion:nil];
    }

}
- (void)refreshWithModel:(Model *)model WithIndexPath:(NSIndexPath*)path
{
    
    
    

    
    
    _indexPath=path;
   
    _model=model;
    
    UIImage* placeholderImage=[UIImage imageNamed:@"placeholderImage"];

    UIImage* userAvatar_url_placeholder=[UIImage imageNamed:@"userAvatar_url_placeholder"];

    NSDictionary* topic=model.topic;
    
    _topicTitle.text=topic[@"title"];
    
    NSDictionary* user=model.topic[@"user"];

    [_userAvatar_url sd_setImageWithURL:[NSURL URLWithString:user[@"avatar_url"]] placeholderImage:userAvatar_url_placeholder];

    [_cover_image_url sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholderImage:placeholderImage];
    
    CAGradientLayer *_gradientLayer;
    
    _gradientLayer = [CAGradientLayer layer];  // 设置渐变效果
    _gradientLayer.borderWidth = 0;
    
    _gradientLayer.frame = _cover_image_url.bounds;
    _gradientLayer.colors = [NSArray arrayWithObjects:
                             (id)[[UIColor clearColor] CGColor],
                             (id)[JZJLIGHTGRAY CGColor], nil];
    _gradientLayer.startPoint = CGPointMake(0.5, 0.8);
    _gradientLayer.endPoint = CGPointMake(0.5, 1.0);
    
    [_cover_image_url.layer insertSublayer:_gradientLayer atIndex:0];
    
    
    
    _nickname.text=model.topic[@"user"][@"nickname"];
    
    
    
    _title.text=model.title;

   
    _comments_count.text=[NSString stringWithFormat:@"%@",model.comments_count];

    _shared_count.text=[NSString stringWithFormat:@"%@",model.shared_count];
    _likes_count.text=[NSString stringWithFormat:@"%@",model.likes_count];

    
    
    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];
    
    
    [mutableArray enumerateObjectsUsingBlock:^(NSNumber* obj, NSUInteger idx, BOOL *stop) {
        
        if ([model.topic[@"id"] isEqualToNumber:obj]) {
            _isExisted=YES;

            JZJLog(@"aaaaa%d",_isExisted);
            _likes_count.text=[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[_likes_count.text integerValue]+1]];
            JZJLog(@"xxxx%@",[userdefault objectForKey:IDArray]);


            _goodHand.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];
            

        }else
            
        {
        
        
            _likes_count.text=[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[_likes_count.text integerValue]-1]];
            
            
            _goodHand.image=[UIImage imageNamed:@"quickLook2.png"];

            
        
        }
        
    }];
    
    
    
    [userdefault synchronize];
    JZJLog(@"%@",[userdefault objectForKey:IDArray]);


   
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
